package com.weitu.permission.dictionaryData.controller;

import com.weitu.base.controller.BaseController;
import com.weitu.paging.DataGridPagination;
import com.weitu.permission.dictionaryData.entity.DictionaryData;
import com.weitu.permission.dictionaryData.service.DictionaryDataService;
import com.weitu.plugin.PageView;
import com.weitu.util.AjaxResult;
import com.weitu.util.DateTimeUtil;
import com.weitu.util.IDGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by smp on 2017/6/15.
 */
@Controller
@RequestMapping(value = "permission/dictionaryData")
public class DictionaryDataController extends BaseController{
    private static final String MODULE_BATH_PATH=BASE_JSP_PATH+"permission/dictionaryData";
    private final DictionaryDataService dictionaryDataService;

    @Autowired
    public DictionaryDataController(DictionaryDataService dictionaryDataService){
        this.dictionaryDataService=dictionaryDataService;
    }

    /**
     * 查询列表
     * @return
     */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public String list(){
        return MODULE_BATH_PATH+"/list";
    }

    /**
     * 查列表
     * @param pId
     * @param name
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/list",method = RequestMethod.POST)
    public Object listAjax(String pId,String name){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        Map<String, Object> paramMap = new HashMap<String, Object>();
        putParametersIntoMap(paramMap);

        int count=this.dictionaryDataService.queryCount(paramMap);

        DataGridPagination dataGridPagination = DataGridPagination.getInstance(request, count);

        PageView pageView = dataGridPagination.getPageView();//生成pageView分页对象
        paramMap.put("pageView", pageView);
        List<DictionaryData> dataList = this.dictionaryDataService.queryList(paramMap);//获取列表数据

        return dataGridPagination.flushDataGridData(dataList);
    }

    /**
     * 查树
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/treeList",method = RequestMethod.POST)
    public List<DictionaryData> treeList(){
        Map<String,Object> map=new HashMap<String, Object>();
       return this.dictionaryDataService.queryTreeList(map);
    }

    /**
     * 添加
     * @param pId
     * @return
     */
    @RequestMapping(value="/add",method = RequestMethod.GET)
    public ModelAndView add(String pId,String name){
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("pId",pId);
        modelAndView.addObject("name",name);
        modelAndView.setViewName(MODULE_BATH_PATH+"/add");
        return modelAndView;

    }

    /**
     * 数据字典添加
     * @param dictionaryData
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public AjaxResult addAjax(DictionaryData dictionaryData){
        dictionaryData.setId(IDGenerator.getId());
        dictionaryData.setAddUserId(BaseController.getSessionUser().getId());
        dictionaryData.setAddTime(DateTimeUtil.formatterDateTime());
        dictionaryData.setDelState(0);
        int count=this.dictionaryDataService.insert(dictionaryData);
        if(count>0){
            return AjaxResult.success("插入成功",dictionaryData);
        }else {
            return AjaxResult.fail("插入失败");
        }
    }
    /**
     * 判断code重复
     * @param code
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/judgement",method = RequestMethod.POST)
    public AjaxResult judgement(String code){
        int count=this.dictionaryDataService.queryCountByCode(code);
        if(count>0){
            return AjaxResult.fail("code 重复");
        }else {
            return AjaxResult.success("code 可以添加");
        }
    }

    /**
     * 跳到编辑页面
     * @param id
     * @return
     */
    @RequestMapping(value="/edit",method = RequestMethod.GET)
    public ModelAndView edit(String id){
        ModelAndView modelAndView=new ModelAndView();
        DictionaryData dictionaryData=this.dictionaryDataService.queryById(id);
        modelAndView.addObject("dictionaryData",dictionaryData);
        modelAndView.setViewName(MODULE_BATH_PATH+"/edit");
        return modelAndView;
    }

    /**
     * 编辑
     * @param dictionaryData
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    public AjaxResult editAjax(DictionaryData dictionaryData){
        dictionaryData.setUpdateUserId(BaseController.getSessionUser().getId());
        dictionaryData.setUpdateTime(DateTimeUtil.formatterDateTime());
        int count=this.dictionaryDataService.update(dictionaryData);
        System.out.print(count+"---------------------------------------------------------");
        if(count>0){
            return AjaxResult.success("编辑成功",dictionaryData);
        }else {
            return AjaxResult.fail("编辑失败");
        }
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    public AjaxResult delete(String id){
        int count=this.dictionaryDataService.delete(id);
        if(count>0){
           return AjaxResult.success("删除成功");
        }else {
           return AjaxResult.fail("删除失败");
        }
    }



    //外部接口

    /**
     * 根据code查下级数据字典列表
     * @param code
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryDictionaryDataByParentCode",method = RequestMethod.POST)
    public List<DictionaryData> queryDictionaryDataByParentCode(String code){
        return this.dictionaryDataService.queryDictionaryDataByParentCode(code);
    }

    /**
     * 根据parenId查下级数据字典列表
     * @param parenId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryDictionaryDataByParentId",method = RequestMethod.POST)
    public List<DictionaryData> queryDictionaryDataByParentId(String parenId){
        return this.dictionaryDataService.queryListAll(parenId);
    }

    /**
     * 根据code查数据字典
     * @param code
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryDictionaryDataByCode",method = RequestMethod.POST)
    public DictionaryData queryDictionaryDataByCode(String code){
        return this.dictionaryDataService.queryDictionaryDataByCode(code);
    }

    /**
     * 根据id查数据字典
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryDictionaryDataById",method = RequestMethod.POST)
    public DictionaryData queryDictionaryDataById(String id){
        return this.dictionaryDataService.queryById(id);
    }
}
